﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

namespace EducationManagementSystem.Zr
{
    public partial class StudentChengJiManage : System.Web.UI.Page
    {
        static string drop = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Bind();//标题
                Bind2();
            }
        }

        private void Bind()
        {
            SqlConnection conn = DB.Conn();
            conn.Open();
            string sql = "select a.schoolname from School a,Zhuren b where zhurenID='zr001' and a.schoolid=b.schoolid";
            SqlCommand cmd = new SqlCommand(sql, conn);
            DataSet ds = new DataSet();
            SqlDataAdapter sda = new SqlDataAdapter(cmd);
            sda.Fill(ds);
            Label1.Text = ds.Tables[0].Rows[0][0].ToString() + "的成绩列表";//标题

        }
        /// <summary>
        /// 下拉绑定
        /// </summary>
        private void Bind2()
        {

            SqlConnection conn = DB.Conn();
            conn.Open();
            string sql = "select b.className,b.classID from Zhuren a,class b where zhurenID='zr001' and a.schoolID=b.schoolID";
            SqlCommand cmd = new SqlCommand(sql, conn);
            SqlDataReader ds = cmd.ExecuteReader();   //读取形式，也可以是用Dataset的形式
            DropDownList1.DataSource = ds;
            DropDownList1.DataTextField = "className";   //绑定班级
            DropDownList1.DataValueField = "classID"; //绑定班级编号
            DropDownList1.DataBind();
        }
        /// <summary>
        /// 通过scoreID删除成绩
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string s = e.CommandArgument.ToString();
            if (e.CommandName == "de")
            {
                SqlConnection conn = DB.Conn();
                conn.Open();
                string sql = "delete from Score where scoreID='" + s + "' ";
                SqlCommand cmd = new SqlCommand(sql, conn);
                int a = cmd.ExecuteNonQuery();
                if (a > 0)
                {
                    this.Page.RegisterStartupScript(" ", "<script>alert(' 删除成功 '); </script> ");
                    Bind4();
                }
                else
                {
                    this.Page.RegisterStartupScript(" ", "<script>alert('删除失败'); </script> ");
                }
            }
        }
        /// <summary>
        /// 分
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            Bind4();
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Button1_Click(object sender, EventArgs e)
        {
            Response.Redirect("ChengJiEdit.aspx");
        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            drop = DropDownList1.SelectedValue;
            Bind4();
            
            //for (int i = 0; i < DropDownList1.Items.Count; i++)
            //{
            //    if (DropDownList1.SelectedIndex == i)
            //    {
            //        drop = DropDownList1.Items[i].Value.ToString();
            //        Bind4();
            //    }
            //    break;
            //}
        }

        protected void Bind4()
        {
            SqlConnection conn = DB.Conn();
            conn.Open();
            string sql = "select d.scoreID,a.stuID,a.stuName,b.courseName,b.courseCount,d.courseScore  from Student a ,Course b,Class c,Score d,School g where c.classID='" + drop + "' and a.classID=c.classID and b.schoolID=c.schoolid and b.courseid=d.courseid ";
            SqlCommand cmd = new SqlCommand(sql, conn);
            DataSet ds = new DataSet();
            SqlDataAdapter sda = new SqlDataAdapter(cmd);
            sda.Fill(ds);
            GridView1.DataSource = ds.Tables[0];
            GridView1.DataBind();
        }

       
    }
}